local skel = fk.CreateSkill {
  name = "rmt__fuzhi",
}

Fk:loadTranslationTable{
  ["rmt__fuzhi"] = "缚枝",
  [":rmt__fuzhi"] = "隐匿技，你登场的回合结束时，<a href='enter_hidden_href'>进入隐匿状态</a>并随机获得一个隐匿技"..
  "（若不为本轮首次发动此技能，你先失去1点体力），然后本局游戏隐匿技能库中加入一个特殊隐匿技〖破桎〗。",

  ["$rmt__fuzhi1"] = "身是吞舟之鱼，岂可游于枝流？",
  ["$rmt__fuzhi2"] = "我有大翼垂天，当负长风万里。",
}

local DIY = require "packages.diy_utility.diy_utility"

local U = require "packages.utility.utility"

-- 获取所有可用的隐匿技
local getHiddenSkill = function ()
  local ret = {}
  local whitelist_pack = {"ol"}
  -- 用于存放存在BUG的技能
  local blacklist_skill = {"efengqi__zongyi"}
  for g, general in pairs(Fk.generals) do
    if Fk:canUseGeneral(g)
    or (general.package and general.package.extensionName and
     (table.contains(whitelist_pack, general.package.extensionName) or table.contains(whitelist_pack, general.package.name))) then
      for _, skill_name in ipairs(general:getSkillNameList()) do
        local skill = Fk.skills[skill_name]
        if skill:hasTag(Skill.Hidden) and not table.contains(blacklist_skill, skill.name) then
          table.insert(ret, skill.name)
        end
      end
    end
  end
  return ret
end

skel:addEffect(fk.TurnEnd, {
  anim_type = "special",
  events = {fk.TurnEnd},
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and (player:getMark("rmt__fuzhi-turn") > 0 or
      table.contains(player:getTableMark("@[:]rmt__zhuozheng"), skel.name))
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if player:usedSkillTimes(skel.name, Player.HistoryRound) > 1 then
      room:loseHp(player, 1, skel.name)
      if player.dead then return end
    end
    if player:getMark("__hidden_general") == 0 and player:getMark("__hidden_deputy") == 0 then
    DIY.enterHidden(player)
    end
    if player.dead then return end
    room.tag[skel.name] = room.tag[skel.name] or {}
    if #room.tag[skel.name] == 0 then
      room.tag[skel.name] = getHiddenSkill()
    elseif player:usedSkillTimes(skel.name, Player.HistoryGame) == 2 then
      table.insertIfNeed(room.tag[skel.name], "rmt__pozhi")
    end
    local skills = table.filter(room.tag[skel.name], function (name)
      return not player:hasSkill(name, true)
    end)
    if #skills > 0 then
      room:handleAddLoseSkills(player, table.random(skills), nil, true, false)
    end
  end,
})

skel:addEffect(U.GeneralAppeared, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:hasShownSkill(skel.name, true)
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "rmt__fuzhi-turn", 1)
  end,
})

return skel
